<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Login_model extends CI_Model{
    function __construct(){
        parent::__construct();
        $this->load->helper('security');
        $this->load->library('common');
        $this->load->library('email');
    }
    
    public function checkemail($email = '') {
    	$this->db->where('email_id', $email);
    	$query = $this->db->get('members');
    	return $query->num_rows;
    }
    
    public function validate(){
        // grab user input
        $username = $this->security->xss_clean($this->input->post('username'));
        $password = do_hash($this->security->xss_clean($this->input->post('password')), 'md5');
        // Prep the query
        $this->db->where('username', $username);
        $this->db->where('password', $password);
        
        // Run the query
        $query = $this->db->get('users');
        // Let's check if there are any results
        if($query->num_rows == 1)
        {
            // If there is a user, then create session data
            $row = $query->row();
            $data = array(
                    'userid' => $row->id,
            		'user_role' => $row->role,
                    'validated' => true
                    );
            $this->session->set_userdata($data);
            return true;
        }
        // If the previous process did not validate
        // then return false.
        return false;
    }
    
    public function retrievePassword() {
    	$username = $this->security->xss_clean($this->input->post('uname'));
    	$this->load->database();
    	$this->db->select('*');
    	$this->db->from('users');
    	$this->db->join('members', 'members.id = users.id');
    	$this->db->where('username', $username);
    	$query = $this->db->get();
    	$details = $query->result();
    	if($query->num_rows == 1)
    	{
    		$new = $this->common->generateRandomString();
    		$data = array(
    				'password' => do_hash($new, 'md5')
    		);
    		$this->db->where('username', current($details)->username);
    		$this->db->update('users', $data);
    		$this->email->from('sudarshankushal@gmail.com', 'Sudarshan Subramanian');
    		$this->email->to(current($details)->email_id);
    		$this->email->subject('Requesting For New Password');
    		$this->email->message("Dear Alumni<br> The New Password is ". $new);
    		$this->email->send();
    		return 1;
    	}
    }
    
    public function retrieveUsername() {
    	$email = $this->security->xss_clean($this->input->post('email'));
    	$this->load->database();
    	$this->db->select('*');
    	$this->db->from('members');
    	$this->db->join('users', 'members.id = users.id');
    	$this->db->where('email_id', $email);
    	$query = $this->db->get();
    	$details = $query->result();
    	if($query->num_rows == 1)
    	{
    		$this->email->from('sudarshankushal@gmail.com', 'Sudarshan Subramanian');
    		$this->email->to(current($details)->email_id);
    		$this->email->subject('Requesting For Username');
    		$this->email->message("Dear Alumni<br> Your Username is ". current($details)->username);
    		$this->email->send();
    		return 1;
    	}
    }
}